package com.supermap.search.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.supermap.search.R;
import com.supermap.search.Url_name;
import com.supermap.search.javabean.XunJianImg;
import com.supermap.search.javabean.XunJianJiLu;
import com.supermap.search.utils.SpUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.CacheMode;
import com.yolanda.nohttp.rest.RequestQueue;
import com.yolanda.nohttp.rest.Response;
import com.yolanda.nohttp.rest.SimpleResponseListener;
import com.yolanda.nohttp.rest.StringRequest;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wyw on 2017/3/10.
 * 巡检记录的界面
 */

public class ZiYuanListActivity extends AppCompatActivity {

    @BindView(R.id.tv_allclear)
    TextView tvAllclear;
    @BindView(R.id.lv_xunjianjilu)
    ListView lvXunjianjilu;
    @BindView(R.id.swipe_ly)
    SwipeRefreshLayout SwipeLy;
    private RequestQueue requestQueue;
    private StringRequest stringRequest;
    private XunJianJiLu xunjianjilu;
    private StringRequest stringRequestimg;
    private Gson gson;
    private XunJianImg xunJianImg;
    private Myadapter myadapter;
    private SharedPreferences sp;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ziyuanlist);
        ButterKnife.bind(this);
        sp = getSharedPreferences(Url_name.SP, 0);
//        初始化数据
        initdata();
//       初始化下拉刷新
        initSwipeLy();


    }

    private void initSwipeLy() {
        SwipeLy.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initdata();
            }
        });
        SwipeLy.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
//        SwipeLy.setDistanceToTriggerSync(400);// 设置手指在屏幕下拉多少距离会触发下拉刷新
        SwipeLy.setSize(SwipeRefreshLayout.DEFAULT); // 设置圆圈的大小
    }

    private void initdata() {
        requestQueue = NoHttp.newRequestQueue();
        if (sp.getString("name", "").equals("master")) {
            //所有巡检记录
            stringRequest = new StringRequest(Url_name.ALL_XUNJIANHISTORY, RequestMethod.GET);
        } else {
            //所有单个巡检记录
            stringRequest = new StringRequest(Url_name.USER_XUNJIANHISTORY + sp.getString("name", ""), RequestMethod.GET);
        }
        stringRequest.setCacheMode(CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE);
        requestQueue.add(1, stringRequest, new SimpleResponseListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                gson = new Gson();
                xunjianjilu = gson.fromJson(response.get(), XunJianJiLu.class);
                if (xunjianjilu.getResultList() != null && xunjianjilu.getResultList().size() > 0) {
                    tvAllclear.setVisibility(View.GONE);
                    myadapter = new Myadapter();
                    lvXunjianjilu.setAdapter(myadapter);
                    SwipeLy.setRefreshing(false);// 停止刷新
                    Toast.makeText(ZiYuanListActivity.this, "已更新", Toast.LENGTH_SHORT).show();
                    lvXunjianjilu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                            Intent intent = new Intent(ZiYuanListActivity.this, YiShangChuanXiangQingActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("user", xunjianjilu.getResultList().get(position));
                            intent.putExtras(bundle);
//                            添加已经阅读的标记
                            SpUtils.setXunJvianJilu(ZiYuanListActivity.this,xunjianjilu.getResultList().get(position).getPKID());
                                    myadapter.notifyDataSetChanged();
                                    startActivity(intent);
                        }
                    });
                } else {
                    tvAllclear.setText("没有巡检记录");
                    SwipeLy.setRefreshing(false);// 停止刷新
                }

            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                tvAllclear.setText("联网失败了");
                SwipeLy.setRefreshing(false);// 停止刷新
            }
        });
    }

    private class Myadapter extends BaseAdapter {

        @Override
        public int getCount() {
            return xunjianjilu.getResultList().size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = null;
            ZiYuanListActivity.ViewHolder viewHolder = null;
            if (convertView == null) {
                view = View.inflate(ZiYuanListActivity.this, R.layout.item_lv_weishangchuan, null);
                viewHolder = new ZiYuanListActivity.ViewHolder(view);
                view.setTag(viewHolder);
            } else {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            }
/*            stringRequestimg = new StringRequest(Url_name.IMG_XUNJIAN + xunjianjilu.getResultList().get(position).getPKID());
            requestQueue.add(2, stringRequestimg, new SimpleResponseListener<String>() {
                @Override
                public void onSucceed(int what, Response<String> response) {
                    xunJianImg = gson.fromJson(response.get(), XunJianImg.class);
                    if (xunJianImg.getResultList() != null && xunJianImg.getResultList().size() > 0) {
                        xunjianjilu.getResultList().get(position).setCENTERX(xunJianImg.getResultList().get(0).getAttachementID());
                    }
                }

                @Override
                public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {

                }
            });
            Glide.with(XunJianJiLuActivity.this)
                    .load(Url_name.IMG_REAL_XUNJIAN + xunjianjilu.getResultList().get(position).getCENTERX())
                    .into(viewHolder.ivNoupxunjian);*/
            viewHolder.tvWentiqingkuang.setText("问题情况:" + xunjianjilu.getResultList().get(position).getPROBLEMDESC());
            viewHolder.tvYanzhongchengdu.setText("巡检人:" + xunjianjilu.getResultList().get(position).getREPORTUSERID());
            viewHolder.tvTime.setText(xunjianjilu.getResultList().get(position).getREPORTTIME());
            //                这里判断是否是已经读过文件
            List<String> list =SpUtils.getXunJianJilu(ZiYuanListActivity.this);
                if (list.contains(xunjianjilu.getResultList().get(position).getPKID())) {
                    viewHolder.tvYanzhongchengdu.setTextColor(Color.RED);
                }else {
                    viewHolder.tvYanzhongchengdu.setTextColor(Color.BLACK);
                }
            Log.i("heheh",""+position);
            return view;
        }


    }

    static class ViewHolder {
        @BindView(R.id.iv_noupxunjian)
        ImageView ivNoupxunjian;
        @BindView(R.id.tv_yanzhongchengdu)
        TextView tvYanzhongchengdu;
        @BindView(R.id.tv_wentiqingkuang)
        TextView tvWentiqingkuang;
        @BindView(R.id.tv_time)
        TextView tvTime;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
